const router = require('koa-router')() // koa2路由实现需要依赖koa-router插件
const {
  getList,
  getDetail,
  newBlog,
  updateBlog,
  delBlog
} = require('../controller/blog')
const {
  SuccessModel,

  ErrorModel
} = require('../model/resModel')
const  loginCheck  = require('../middleware/loginCheck')
router.prefix('/api/blog') // 设置前缀

router.get('/list', async function (ctx, next) {
  const author = ctx.query.author || ''
  const keyword = ctx.query.keyword || ''

  if(ctx.query.isadmin) {
    // 管理员界面
    if(ctx.session.username == null){
      // 未登录
      ctx.body = new ErrorModel('未登录')
      // res.json(
      //   new ErrorModel('未登录')
      // )
      return
    }
    // 强制查询自己的博客
    author = ctx.session.username
  }
  const result = await getList(author, keyword)
  ctx.body = new SuccessModel(result)
  // return result.then(listData => {
  //   ctx.body = new SuccessModel(listData)
  // })
})

router.get('/detail', async function(ctx, next) {
  const result = await getDetail(ctx.query.id)
  ctx.body = new SuccessModel(result)
})

// router.post('/new', async function(ctx, next) {

// })

module.exports = router
